package com.lisp.algorithm.util;

import java.util.Arrays;

public class SortUtil {

    public static void test(IntArrayConsumer sortFunction) {
        int nums1[] = {100};
        doTest(nums1, sortFunction);
        
        int nums2[] = {100, 1};
        doTest(nums2, sortFunction);
        
        int nums22[] = {1, 100};
        doTest(nums22, sortFunction);
        
        int nums3[] = {1, 100, 1};
        doTest(nums3, sortFunction);
        
        int nums32[] = {100, 1, 100};
        doTest(nums32, sortFunction);
        
        int nums01[] = {5, 3, 8, 2, 5, 9, 20, 11, 2, 13};
        doTest(nums01, sortFunction);
        
        int nums02[] = {30, 88, 88, 50, 24, 1, 100};
        doTest(nums02, sortFunction);
        
        int nums03[] = {100, 30, 88, 88, 50, 24, 1, 1};
        doTest(nums03, sortFunction);
        
        int nums04[] = {1, 1, 100, 30, 88, 88, 50, 24, 1, 1, 20, 1};
        doTest(nums04, sortFunction);
    }
    
    private static void doTest(int[] nums, IntArrayConsumer sortFunction) {
        int[] copyOf = Arrays.copyOf(nums, nums.length);
        sortFunction.consume(copyOf);
        Arrays.sort(nums);
        if(!Arrays.equals(nums, copyOf)) {
            System.out.println(Arrays.toString(copyOf));
            throw new RuntimeException("not sorted");
        }
    }
    
    public static interface IntArrayConsumer {
        void consume(int a[]);
    }
    
    public static void swap(int a[], int i, int j) {
        int tmp = a[i];
        a[i] = a[j];
        a[j] = tmp;
    }
}
